﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using DeviceFinder.DAL;
using DeviceFinder.Models;

namespace DeviceFinder.Controllers
{
    public class CommandController : ApiController
    {
        // GET api/command
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/command/5
        public List<CMModel> Get(string AccessToken)
        {
            Dictionary<string, TokenModel> tkDict = HttpContext.Current.Application[OAuthManager.name] as Dictionary<string, TokenModel>;
            TokenModel tk = null;
            try
            {
                tk = tkDict[AccessToken];
                if (tk != null )
                {
                    CommandDAL cmDAL = new CommandDAL();
                    List<COMMAND> commandS = cmDAL.GetAllCommandByDeviceIDUndone(tk.DeviceId).ToList();
                    List<CMModel> cmModel = new List<CMModel>();
                    foreach (COMMAND c in commandS)
                    {
                        CMModel tempModel = new CMModel() { Id = c.ID ,Opcode = c.OPCODE, Param = c.PARAM, Status = c.STATUS};
                        cmModel.Add(tempModel);

                    }
                    return cmModel;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }

        // POST api/command
        public void Post([FromBody]string value)
        {
        }

        // PUT api/command/5
        public string Put(int id, [FromBody]Command value, string AccessToken)
        {
            string tokenStr = AccessToken;
            Dictionary<string, TokenModel> tkDict = HttpContext.Current.Application[OAuthManager.name] as Dictionary<string, TokenModel>;
            TokenModel tk = null;
            try
            {
                tk = tkDict[tokenStr];
                if (tk != null && tk.isAccessToken == true)
                {
                    CommandDAL cmDAL = new CommandDAL();
                    COMMAND cm =  cmDAL.GetCommandByID(id);
                    if (cm.STATUS != "done")
                    {
                        switch (value.Status)
                        {
                            case 1:
                                cm.STATUS = "done";

                                break;
                            case 0:
                                cm.STATUS = "undone";
                                break;
                            case -1:
                                cm.STATUS = "error";
                                break;
                            default:
                                break;
                        }
                        cmDAL.EditCommand(cm);
                        return "1";
                    }

                    return "0";
                }
                else
                {
                    return "-1";
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        // DELETE api/command/5
        public void Delete(int id)
        {
        }
    }
}
